package com.zz.concurrent.base;

/**
 * Author: zhangzhen
 * DATE: Created in 2018/8/14 10:12
 * Description:
 */
public class Stage extends Thread {

    public void run() {

        System.out.println("欢迎观看");
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("帷幕拉开");
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("隋朝末年,隋军与起义军作战");
        ArmyRunable armyTaskOfSuiDynasty = new ArmyRunable();
        ArmyRunable armyTaskOfRevolt = new ArmyRunable();

        // 使用Runnable接口创建线程
        Thread armyOfSuiDynasty = new Thread(armyTaskOfSuiDynasty, "隋军");
        Thread armyOfRevolt = new Thread(armyTaskOfSuiDynasty, "起义军");

        // 启动线程
        armyOfSuiDynasty.start();
        armyOfRevolt.start();
        // 舞台线程休眠
        try {
            Thread.sleep(50);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("半路杀出个程");

        Thread mrCheng = new KeyPersonThread();
        mrCheng.setName("程咬金");

        System.out.println("结束战争");
        // 停止线程的方法
        armyTaskOfSuiDynasty.keepRunning = false;
        armyTaskOfRevolt.keepRunning = false;

        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        mrCheng.start();

        try {
            mrCheng.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("战争结束了,安民");
        System.out.println("谢谢观看");
    }

    public static void main(String[] args) {
        new Stage().start();
    }
}
